﻿

var _option = {
    title: {
        text: ""
    },
    tooltip: {
        //show: false,
        trigger: 'axis'
    },
    //legend: {
    //    x:'right',
    //    data: ['寿命损耗'
    //        //, '发电量'
    //    ]
    //},
    xAxis: [
        {
            type: 'category',
            data: [],
            axisPointer: {
                type: 'shadow'
            }
        }
    ],
    yAxis: [
        {
            type: 'value',
            name: '寿命损耗(年)',
            //min: 0,
            //max: 0.5,
            //interval: 0.1,
            axisLabel: {
                formatter: '{value}'
            }
        }
        //,
        //{
        //    type: 'value',
        //    name: '发电量',
        //    min: 0,
        //    max: 1000,
        //    interval: 200,
        //    axisLabel: {
        //        formatter: '{value} kWh'
        //    }
        //}
    ],
    series: [
        {
            name: '寿命损耗',
            type: 'bar',
            barWidth: '50%',
            data: []
        }
        //,
        //{
        //    name: '发电量',
        //    type: 'bar',
        //    yAxisIndex: 1,
        //    data: [532, 494, 300, 679]
        //}
    ]
}


/* 叶片 */
var eCharts_com1 = [];
var options_com1 = [{}, {}, {}];

//绘制叶片的HTML
function drawCom1_HTML() {
    $("#chart-wrap").append("\
                <!--叶片1-->\
                <div class='col-md-6'>\
                    <div class='panel panel-chart'>\
                        <div class='panel-heading'>叶片1</div>\
                        <div class='panel-body'>\
                            <div class='chart-3' id='chart1'></div>\
                        </div>\
                    </div>\
                </div>\
                <!--叶片2-->\
                <div class='col-md-6'>\
                    <div class='panel panel-chart'>\
                        <div class='panel-heading'>叶片2</div>\
                        <div class='panel-body'>\
                            <div class='chart-3' id='chart2'></div>\
                        </div>\
                    </div>\
                </div>\
                <!--叶片3-->\
                <div class='col-md-6'>\
                    <div class='panel panel-chart'>\
                        <div class='panel-heading'>叶片3</div>\
                        <div class='panel-body'>\
                            <div class='chart-3' id='chart3'></div>\
                        </div>\
                    </div>\
                </div>");

    eCharts_com1 = [
        echarts.init(document.getElementById('chart1')),
        echarts.init(document.getElementById('chart2')),
        echarts.init(document.getElementById('chart3'))
    ];
    options_com1.forEach(function (opt) {
        $.extend(true, opt, _option);
    })

    eCharts_com1.forEach(function (e) {
        e.showLoading();
    })
}

//绘制叶片图表，times为x轴，chartsInfo为y轴
function drawCom1_Chart(times, chartsInfo, turbineId) {
    var blade1 = [];
    var blade2 = [];
    var blade3 = [];

    if (chartsInfo != null) {
        blade1 = getDiffArray(chartsInfo[turbineId][1]); //叶片componentId=1/2/3,硬编码
        blade2 = getDiffArray(chartsInfo[turbineId][2]);
        blade3 = getDiffArray(chartsInfo[turbineId][3]);
    }

    options_com1[0]["title"]["text"] = turbineId + "#风机 叶片1 寿命损耗";
    options_com1[0]["xAxis"][0]["data"] = times;
    options_com1[0]["series"][0]["data"] = blade1;

    options_com1[1]["title"]["text"] = turbineId + "#风机 叶片2 寿命损耗";
    options_com1[1]["xAxis"][0]["data"] = times;
    options_com1[1]["series"][0]["data"] = blade2;

    options_com1[2]["title"]["text"] = turbineId + "#风机 叶片2 寿命损耗";
    options_com1[2]["xAxis"][0]["data"] = times;
    options_com1[2]["series"][0]["data"] = blade3;

    eCharts_com1.forEach(function (e, index) {
        e.hideLoading();
        e.setOption(options_com1[index]);
    })
}


/* 主轴，塔顶，塔底 */
var eCharts_com2 = null;
var options_com2 = {};

//绘制(主轴，塔顶，塔底)的HTML
function drawCom2_HTML(componentId) {
    var componentName = getComponentName(componentId);
    $("#chart-wrap").append(
        "<div class='panel panel-chart'>"
        + "<div class='panel-heading'>"+ componentName +"</div>"
        + "<div class='panel-body'>"
        + "<div class='chart-2' id='chart1'></div>"
        + "</div>"
        + "</div>"
    );

    eCharts_com2 = echarts.init(document.getElementById('chart1'));
    $.extend(true, options_com2, _option);

    eCharts_com2.showLoading();
}

//绘制叶片图表，times为x轴，chartsInfo为y轴
function drawCom2_Chart(times, chartsInfo, turbineId,componentId) {
    var componentData = [];
    if (chartsInfo != null) {
        componentData = getDiffArray(chartsInfo[turbineId][componentId]); 
    }

    var componentName = getComponentName(componentId);

    options_com2["title"]["text"] = turbineId + "#风机 "+ componentName +" 寿命损耗";
    options_com2["xAxis"][0]["data"] = times;
    options_com2["series"][0]["data"] = componentData;

    eCharts_com2.hideLoading();
    eCharts_com2.setOption(options_com2);
}

function getDiffArray(oldArray) {
    var newArray = [];
    var last = 0;
    for (var i = 0; i < 5; i++) {
        if (i!=0){
            if (last!=0 && oldArray[i]!=0) {
                newArray.push((last - oldArray[i]).toFixed(4));
            }
            else {
                newArray.push(0);
            }
        }
        last = oldArray[i];
    }
    return newArray;
}

function getComponentName(componentId) {
    var componentName='';
    switch (componentId) {
        case '4':
            componentName = "主轴";
            break;
        case '5':
            componentName = "塔顶";
            break;
        case '7':
            componentName = "塔底";
            break;
        default:
    }
    return componentName;
}